package com.slkj.shilixiaoyuanapp.ui.main.community;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.CommunityNewMessageEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.MySwipeRefreshLayout;
import com.slkj.shilixiaoyuanapp.view.StateLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.PAGE_COUNT;

@ActivityInfo(layout = R.layout.activity_new_message, title = "动态")
public class NewMessageActivity extends BaseActivity {
    @BindView(R.id.recycer)
    RecyclerView recycerType;
    BaseQuickAdapter adapter;
    @BindView(R.id.layout_root)
    StateLayout stateLayout;
    @BindView(R.id.swipeLayout)
    MySwipeRefreshLayout refreshLayout;
    private int page = 1;
    private int count = PAGE_COUNT;
    List<CommunityNewMessageEntity> toolWorkListEntityList = new ArrayList<>();
    @Override
    protected void init() {
        recycerType.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        recycerType.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseQuickAdapter<CommunityNewMessageEntity, BaseViewHolder>(R.layout.item_comunity_message, toolWorkListEntityList) {
            @Override
            protected void convert(BaseViewHolder helper, final CommunityNewMessageEntity item) {
                helper.setText(R.id.tv_name, item.getNickName())
                        .setText(R.id.tv_time,item.getIntime())
                        .setText(R.id.tv_context,item.getContent())
                        .itemView.setOnClickListener(v -> {
                    CommunityDetailsActivity__JumpCenter.builder(NewMessageActivity.this).setId(item.getClassDynamicId()).create().go();
                        });
                ImageView imgHead  = helper.getView(R.id.img_left);
                ImageView imgContext  = helper.getView(R.id.img_right);
                Glide.with(NewMessageActivity.this).load(item.getHeadImg()).into(imgHead);
                if (!TextUtils.isEmpty(item.getContentImg())){
                    Glide.with(NewMessageActivity.this).load(item.getContentImg()).into(imgContext);
                    imgContext.setVisibility(View.VISIBLE);

                }else {
                    imgContext.setVisibility(View.GONE);
                }

            }
        };

        adapter.setOnLoadMoreListener(this::loadMore,recycerType);
        recycerType.setAdapter(adapter);
        refresh();
        refreshLayout.setOnRefreshListener(this::refresh);
        stateLayout.setOnReLoadListener(() -> refresh());
    }
    /**
     * 加载更多
     */
    private void loadMore() {
        HttpHeper.get().comunityService()
                .getCommunityMes(page+1, count)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<CommunityNewMessageEntity>>( ) {
                    @Override
                    public void onCallBackSuccess(List<CommunityNewMessageEntity> data) {
                        setData(false,data);
                    }

                    @Override
                    public void onFail(String mes) {
                        adapter.loadMoreFail();
                    }
                });
    }

    /**
     * 刷新
     */
    public void refresh() {
        page = 1;
        //这里的作用是防止下拉刷新的时候还可以上拉加载
        adapter.setEnableLoadMore(false);
        HttpHeper.get().comunityService()
                .getCommunityMes(page, count)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<CommunityNewMessageEntity>>(stateLayout) {
                    @Override
                    public void onCallBackSuccess(List<CommunityNewMessageEntity> data) {
                        if (data.isEmpty()){
                            stateLayout.showEmptyView();
                        }
                        setData(true,data);
                        refreshLayout.setRefreshing(false);
                    }
                    @Override
                    public void onFail(String mes) {
                        super.onFail(mes);
                        refreshLayout.setRefreshing(false);

                    }
                });
    }
    private void setData(boolean isRefresh, List data) {
        page++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            adapter.setNewData(data);
        } else {
            if (size > 0) {
                adapter.addData(data);
            }
        }
        if (size < count) {
            //第一页如果不够一页就不显示没有更多数据布局
            adapter.loadMoreEnd(isRefresh);
        } else {
            adapter.loadMoreComplete();
        }
    }
}
